<template>
  <!-- 列表页面 -->
  <div class="tableSample" v-if="!showList && !showEdit && !showEditAdmin">
    <div class="header">
      <div class="header-left">
        <p class="title">
          商家列表
        </p>
      </div>
      <div class="header-right">
        <lin-search @query="onQueryChange" placeholder="请输入商家名称" />
      </div>
    </div>
    <!-- 表格 -->
    <el-table border v-loading="loading" :data="tableData" stripe>
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="商家详细信息" class="table-expand-title">
            </el-form-item>
            <el-form-item label="商家名称">
              <span>{{ props.row.title }}</span>
            </el-form-item>
            <el-form-item label="商家状态">
              <span v-if="props.row.status === '0'">不展示</span>
              <span v-if="props.row.status === '1'">展示</span>
            </el-form-item>
            <el-form-item label="商家图片">
              <span v-for="(item) in props.row.imgs" :key="item.id">
                 <el-image :src="item.img.url" style="width: 100px"></el-image>
              </span>
            </el-form-item>
            <el-form-item label="商家排序">
              <span>{{ props.row.order_sort }}</span>
            </el-form-item>
            <el-form-item label="商家所在地址">
              <span>{{ props.row.address }}</span>
            </el-form-item>
            <el-form-item label="商家地址所对应坐标">
              <span>{{ props.row.point }}</span>
            </el-form-item>
            <el-form-item label="商家介绍">
              <span v-html="props.row.content"></span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column
              :show-overflow-tooltip="true"
              prop="title"
              label="商家名称"
      >
      </el-table-column>
      <el-table-column
              :show-overflow-tooltip="true"
              prop="address"
              label="商家所在地址"
      >
      </el-table-column>
      <el-table-column
              :show-overflow-tooltip="true"
              prop="order_sort"
              label="排序(值越大越在前)"
              width="160"
      >
      </el-table-column>
      <el-table-column
              :show-overflow-tooltip="true"
              prop="update_time"
              label="更新时间"
              width="170"
      >
      </el-table-column>
      <!-- 操作列 -->
      <el-table-column label="操作" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button
                  v-if="lin_user_id != 1"
                  type="primary"
                  plain
                  size="mini"
                  @click.native.prevent.stop="handleEdit(scope.row)"
          >修改</el-button
          >
          <el-button
              v-if="lin_user_id === 1"
              type="primary"
              plain
              size="mini"
              @click.native.prevent.stop="handleEditAdmin(scope.row)"
          >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="pagination" v-if="total_nums > 0">
      <el-pagination
              @current-change="handleCurrentChange"
              :background="true"
              :page-size="pageCount"
              :current-page="currentPage"
              v-if="refreshPagination"
              layout="total, prev, pager, next, jumper"
              :total="total_nums"
      ></el-pagination>
    </div>
  </div>
  <sj-msg-edit
          v-else-if="showEdit"
          @editClose="editClose"
          :SjMsgId="SjMsgId"
  ></sj-msg-edit>
  <sj-msg-edit-admin
      v-else-if="showEditAdmin"
      @editCloseAdmin="editCloseAdmin"
      :SjMsgId="SjMsgId"
  ></sj-msg-edit-admin>
</template>

<script>
import SjMsg from '@/models/sj-msg'
import LinSearch from '@/components/base/search/lin-search'
import SjMsgEdit from './SjMsgEdit'
import SjMsgEditAdmin from './SjMsgEditAdmin'

import User from '@/lin/models/user'

export default {
  components: {
    LinSearch,
    SjMsgEdit,
    SjMsgEditAdmin,
  },
  data() {
    return {
      tableData: [],
      storeData: [],
      loading: false,
      showEdit: false,
      showList: true,
      showEditAdmin: false,
      lin_user_id: 0,
      orderSort: 0,
      SjMsgId: 0,
      search: {
        name: '',
      },
      // 分页相关
      refreshPagination: true, // 页数增加的时候，因为缓存的缘故，需要刷新Pagination组件
      currentPage: 1, // 默认获取第一页的数据
      pageCount: 20, // 每页50条数据
      total_nums: 0, // 分组内的总数
    }
  },
  computed: {},
  created() {
    this.getAdminUser()
  },
  methods: {
    async getAdminUser() {
      const res = await User.getInformation()
      this.lin_user_id = res.linUserId
      if (this.lin_user_id === 1) {
        this.showList = false
        this.loading = true
        this._getTableData(this.currentPage, this.pageCount)
      } else {
        this.SjMsgId = res.sjMsgId
        this.showEdit = true
      }
    },
    handleEdit(val) {
      this.SjMsgId = val.id
      this.showEdit = true
    },
    handleEditAdmin(val) {
      this.SjMsgId = val.id
      this.showEditAdmin = true
    },
    editClose() {
      this.showEdit = false
      this.loading = true
      this._getTableData(this.currentPage, this.pageCount)
    },
    editCloseAdmin() {
      this.showEditAdmin = false
      this.loading = true
      this._getTableData(this.currentPage, this.pageCount)
    },
    // 切换分页
    async handleCurrentChange(val) {
      this.currentPage = val
      this.loading = true
      this._getTableData(this.currentPage, this.pageCount)
    },
    // 搜索名称
    onQueryChange(query) {
      this.search.name = query.trim()
      if (!query) {
        this._getTableData(1, 10)
        return
      }
      this.loading = true
      this.currentPage = 1
      this._getTableData(this.currentPage, this.pageCount)
    },
    // 获取数据
    async _getTableData(start, count) {
      const res = await SjMsg.getDataList(start, count, {
        title: this.search.name,
      })
      this.tableData = res.data
      this.total_nums = res.count
      this.$nextTick(() => {
        this.loading = false
      })
    },
  },
}
</script>

<style lang="scss" scoped>
  .tableSample {
    padding: 0 0 30px;

    .demo-table-expand {
      font-size: 0;
      margin-left: 30px;
      display: flex;
      flex-wrap: wrap;

      .el-form-item {
        width: 50%;
        margin-right: 0;
        margin-bottom: 0;
        display: flex;
        flex-direction: row;
      }
    }

    .header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 30px;
      border-bottom: 1px solid #dae1ed;

      .header-left {
        float: left;

        .title {
          height: 59px;
          line-height: 59px;
          color: $parent-title-color;
          font-size: 16px;
          font-weight: 500;
        }
      }

      .header-right {
        float: right;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }

    .pagination {
      float: right;
      margin: 20px;
    }
  }
  // dialog
  .tableSample /deep/ .el-dialog__footer {
    //text-align: left;
    padding-left: 30px;
  }

  .tableSample /deep/ .el-dialog__header {
    padding-left: 30px;
  }

  .tableSample /deep/ .el-dialog__body {
    padding: 30px;
  }

  .el-form--inline /deep/ .el-form-item__label{
    width: 180px;
    color: #99a9bf;
  }
  .tableSample .demo-table-expand .table-expand-title {
    width: 100%;
  }
  .table-expand-title /deep/ .el-form-item__label{
    color: #333333;
    font-size: 20px;
    width: 100%;
  }
</style>
